﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DinhMuc.BUL;
using DinhMuc.Public;
using Janus.Windows.GridEX;

namespace DinhMuc.UI.Frm_Form
{
    public partial class DinhMucADD : Form
    {
        public DinhMucADD()
        {
            InitializeComponent();
        }
        tblDinhMucBUL cls = new tblDinhMucBUL();
        DataTable dtPublic = new DataTable();
        int indexRow = 0;
        int index = 0;
        private void button1_Click(object sender, EventArgs e)
        {

        }
        private void DinhMucADD_Load(object sender, EventArgs e)
        {
            indexRow = 0;
            DSDonHang();
            DSNVL();
            DSLoaiDM();
            dtPublic.Columns.Add("ID");
            dtPublic.Columns.Add("soDH");
            dtPublic.Columns.Add("maNVL");
            dtPublic.Columns.Add("DVT");
            dtPublic.Columns.Add("dmThoi");
            dtPublic.Columns.Add("dmTrangL1");
            dtPublic.Columns.Add("dmTrangL2");
            dtPublic.Columns.Add("dmIn");
            dtPublic.Columns.Add("dmChiaCuon");
            dtPublic.Columns.Add("dmCatDan");
            dtPublic.Columns.Add("LoaiDM");
        }
      
        private void DSDonHang()
        {
            tblDonHangBUL cls = new tblDonHangBUL();
            gridEX1.DropDowns["donhang"].DataSource = cls.bangDonHang();

        }
        private void DSLoaiDM()
        {
            tblLoaiDMBUL cls = new tblLoaiDMBUL();
            gridEX1.DropDowns["loaiDM"].DataSource = cls.loaiDM();
        }
        private void DSNVL()
        {
            tblNguyenVatLieuBUL cls = new tblNguyenVatLieuBUL();
            gridEX1.DropDowns["nvl"].DataSource = cls.bangNVL();
        }

        private void gridEX1_AddingRecord(object sender, CancelEventArgs e)
        {
           
        }

        private void gridEX1_CellValueChanged(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
        {
            try
            {
               
                if (e.Column.Key == "maNVL")
                {
                    tblNguyenVatLieuBUL cls = new tblNguyenVatLieuBUL();
                    DataTable dt = cls.bangNVL(gridEX1.GetValue("maNVL").ToString());
                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        string dvt = row["DVT"].ToString();
                        gridEX1.SetValue("DVT", dvt);
                    }
                }
               
                
            }
            catch (Exception)
            {
            }
        }

        private void gridEX1_RecordAdded(object sender, EventArgs e)
        {
            indexRow++;
            DataRow row = dtPublic.NewRow();
            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maNVL"] = gridEX1.GetValue("maNVL").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["dmThoi"] = gridEX1.GetValue("dmThoi").ToString();
            row["dmTrangL1"] = gridEX1.GetValue("dmTrangL1").ToString();
            row["dmTrangL2"] = gridEX1.GetValue("dmTrangL2").ToString();
            row["dmIn"] = gridEX1.GetValue("dmIn").ToString();
            row["dmChiaCuon"] = gridEX1.GetValue("dmChiaCuon").ToString();
            row["dmCatDan"] = gridEX1.GetValue("dmCatDan").ToString();
            row["loaiDM"] = gridEX1.GetValue("loaiDM").ToString();
            row["ID"] = indexRow;
            dtPublic.Rows.Add(row);          

        }
        private void Insert()
        {
            for (int i = 0; i < dtPublic.Rows.Count; i++)
            {
                DataRow row = dtPublic.Rows[i];

                tblDinhMucPublic p = new tblDinhMucPublic();
                p.SoDH = int.Parse(row["soDH"].ToString());
                p.MaNVL = row["maNVL"].ToString();
                p.Dvt = row["DVT"].ToString();
                p.DmThoi = float.Parse("0" + row["dmThoi"].ToString());
                p.DmTrangL1 = float.Parse("0" + row["dmTrangL1"].ToString());
                p.DmTrangL2 = float.Parse("0" + row["dmTrangL2"].ToString());
                p.DmIn = float.Parse("0" + row["dmIn"].ToString());
                p.DmChiaCuon = float.Parse("0" + row["dmChiaCuon"].ToString());
                p.DmCatDan = float.Parse("0" + row["dmCatDan"].ToString());
                p.Loai = int.Parse(row["loaiDM"].ToString());

                cls.themDM(p);
                gridEX1.MoveToNewRecord();
            }
        }


        private void gridEX1_DeletingRecord(object sender, Janus.Windows.GridEX.RowActionCancelEventArgs e)
        {
            //ID = int.Parse(gridEX1.GetValue("ID").ToString());
        }

        private void gridEX1_RecordsDeleted(object sender, EventArgs e)
        {
            //DialogResult dlr = MessageBox.Show("Bạn có chắc muốn xóa không?", "WARNNING",
            //    MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            //if (dlr == DialogResult.OK)
            //{
            //    cls.xoaDM(ID);
            //    DinhMucADD_Load(sender, e);
            //}
            //else DinhMucADD_Load(sender, e);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Insert();
            MessageBox.Show("Thêm thành công");
            this.Close();
        }

        private void gridEX1_RecordUpdated(object sender, EventArgs e)
        {
           
            DataRow row = dtPublic.Rows[gridEX1.CurrentRow.RowIndex];
            row["soDH"] = gridEX1.GetValue("soDH").ToString();
            row["maNVL"] = gridEX1.GetValue("maNVL").ToString();
            row["DVT"] = gridEX1.GetValue("DVT").ToString();
            row["dmThoi"] = gridEX1.GetValue("dmThoi").ToString();
            row["dmTrangL1"] = gridEX1.GetValue("dmTrangL1").ToString();
            row["dmTrangL2"] = gridEX1.GetValue("dmTrangL2").ToString();
            row["dmIn"] = gridEX1.GetValue("dmIn").ToString();
            row["dmChiaCuon"] = gridEX1.GetValue("dmChiaCuon").ToString();
            row["dmCatDan"] = gridEX1.GetValue("dmCatDan").ToString();
            row["loaiDM"] = gridEX1.GetValue("loaiDM").ToString();
            dtPublic.AcceptChanges();
            gridEX1.DataSource = dtPublic;
        }

        private void gridEX1_Click(object sender, EventArgs e)
        {
           
        }

        private void gridEX1_CellEdited(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
        {
           
        }

      
    }
}
